package sg.edu.nus.iss.ooad.jass.controller;

import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.dao.file.AdvertiserDAOPropertiesImpl;
import sg.edu.nus.iss.ooad.jass.dao.file.CompanyDAOPropertiesImpl;
import sg.edu.nus.iss.ooad.jass.dao.file.JobAdvertisementDAOPropertiesImpl;
import sg.edu.nus.iss.ooad.jass.dao.file.JobseekerDAOPropertiesImpl;
import sg.edu.nus.iss.ooad.jass.dao.file.ResponseDAOPropertiesImpl;
import sg.edu.nus.iss.ooad.jass.entity.Advertiser;
import sg.edu.nus.iss.ooad.jass.entity.Company;
import sg.edu.nus.iss.ooad.jass.entity.IJASSEntity;
import sg.edu.nus.iss.ooad.jass.entity.JobAdvertisement;
import sg.edu.nus.iss.ooad.jass.entity.Jobseeker;
import sg.edu.nus.iss.ooad.jass.entity.Response;

/**
 *
 * System Setting Controller. This class will load the application setting during
 * system start-up and save the application setting when it is terminated.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : SystemSettingController.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class SystemSettingController {

        public static final Logger logger = Logger.getLogger(SystemSettingController.class.getName());

        /**
         *  Load application's config during start-up process.
         */
        public static synchronized void loadData() {
                try {
                        //load everything
                        AdvertiserController.cache();
                        CompanyController.cache();
                        JobAdvertisementController.cache();
                        JobseekerController.cache();
                        ResponseController.cache();

                        //build entity/object associations
                        Enumeration<IJASSEntity> enumerations = null;
                        for (enumerations = JobAdvertisementDAOPropertiesImpl.jobadvertisementCache.elements(); enumerations.hasMoreElements();) {
                                JobAdvertisement jobadvertisement = (JobAdvertisement) enumerations.nextElement();

                                //--> Advertiser -- Job Advertisement
                                Advertiser advertiser = (Advertiser) AdvertiserDAOPropertiesImpl.advertiserCache.get(jobadvertisement.getAdvertiserID());
                                if (advertiser != null) {
                                        advertiser.getJobAdvertisement().add(jobadvertisement);
                                }

                                //--> Company -- Job Advertisement
                                Company company = (Company) CompanyDAOPropertiesImpl.companyCache.get(jobadvertisement.getCompanyID());
                                if (company != null) {
                                        company.getJobAdvertisement().add(jobadvertisement);
                                }

                        }
                        enumerations = null;
                        for (enumerations = ResponseDAOPropertiesImpl.responseCache.elements(); enumerations.hasMoreElements();) {
                                Response response = (Response) enumerations.nextElement();

                                //--> Response -- Job Advertisement
                                JobAdvertisement jobadvertisement = (JobAdvertisement) JobAdvertisementDAOPropertiesImpl.jobadvertisementCache.get(response.getJobAdvertisementID());
                                if (jobadvertisement != null) {
                                        jobadvertisement.getResponse().add(response);
                                }

                                //--> Response -- Jobseeker
                                Jobseeker jobseeker = (Jobseeker) JobseekerDAOPropertiesImpl.jobseekerCache.get(response.getJobseekerID());
                                if (jobseeker != null) {
                                        jobseeker.getResponse().add(response);
                                }

                        }
                        enumerations = null;


                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                }
        }

}
